package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    /**
     * 条件查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findListByQuery(ChannelDTO dto) {

        //1.校验业务参数
        //检查分页参数
        dto.checkParam();
        //2. 执行分页查询
        //2.1分页条件对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2 查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //  name 不为空  模糊查询
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
            // status不为空  添加 eq查询
        if (dto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
            // 添加排序
        queryWrapper.orderByAsc(AdChannel::getOrd);

        //执行查询
        IPage<AdChannel> channelIPage = this.page(pageReq, queryWrapper);


        return new PageResponseResult(dto.getPage(),dto.getSize(),channelIPage.getTotal(), channelIPage.getRecords());
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //校验参数
        if (StringUtils.isBlank(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if (adChannel.getName().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        //查询满足条件的数量
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        //保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //返回封装结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //参数校验
        if (adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id为空");
        }
        //根据id查询频道
        AdChannel oldChannel = this.getById(adChannel.getId());
        String name = adChannel.getName();
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel)){
            if (name.length()>10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
            }
            //查询满足条件的数量
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        //修改数据库信息
        this.updateById(adChannel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        AdChannel adChannel = this.getById(id);
        if (adChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效不允许删除");
        }
        //删除数据
        this.removeById(id);
        // int i= 1/0;
        //返回结果
        return ResponseResult.okResult();
    }
}
